package net.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * 读出文件中的数据
 *
 * @author 王霄
 * @date 2021/8/21 17:15
 */
public class FileChannel_02 {
    public static void main(String[] args) throws IOException {
        File file = new File("d:\\aaa.txt");
        var inputStream = new FileInputStream(file);

        //获取通道
        var channel = inputStream.getChannel();

        //创建一个缓冲区
        var buffer = ByteBuffer.allocate((int) file.length());

        //从通道读取数据放到缓冲区
        channel.read(buffer);

        //读写翻转
        buffer.flip();

        //将数据写入channel
        System.out.println(new String(buffer.array()));

        //关闭流
        inputStream.close();
    }
}
